package com.lxy.allappinfo;

import java.io.ByteArrayInputStream;
import java.io.File;
import android.content.pm.PermissionInfo;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionInfo;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailInfoActivity extends Activity {

	private PackageInfo pinfo;
	private GridLayout gridlayout;
	
	private LayoutInflater inflater;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailinfo_layout);
		
		gridlayout=(GridLayout) findViewById(R.id.gridlayout);
		inflater=LayoutInflater.from(this);
		Bundle bundle=getIntent().getExtras();
		pinfo=(PackageInfo) bundle.get("pinfo");
		init();
		
		
		
		
	}
	
	
	public String[] getSingInfo(String packageName) {
		String ss[]=new String[2];
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(
            		packageName, PackageManager.GET_SIGNATURES);
            Signature[] signs = packageInfo.signatures;
            Signature sign = signs[0];
         
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) certFactory
                    .generateCertificate(new ByteArrayInputStream(sign.toByteArray()));
//            String pubKey
            ss[0]= cert.getPublicKey().toString();
//            String signNumber 
            ss[1]= cert.getSerialNumber().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ss;
    }
	
	public void init(){

		TextView tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序icon：");
		gridlayout.addView(tv);
		ImageView imv=(ImageView) inflater.inflate(R.layout.myimage, null);
		imv.setImageDrawable(pinfo.applicationInfo.loadIcon(getPackageManager()));
		gridlayout.addView(imv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序名：");
		gridlayout.addView(tv);
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(pinfo.applicationInfo.loadLabel(getPackageManager()).toString());
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序包名:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(pinfo.packageName);
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("versionName:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(pinfo.versionName);
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("versionCode:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(pinfo.versionCode+"");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序安装时间:");
		gridlayout.addView(tv);
		SimpleDateFormat slf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(slf.format(new Date(pinfo.firstInstallTime)));
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序更新时间:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(slf.format(new Date(pinfo.lastUpdateTime)));
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序sharedUserId:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(pinfo.sharedUserId);
		gridlayout.addView(tv);
		
		
	
		String[] p = null;
		try {
			p = getPackageManager().getPackageInfo(pinfo.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序权限:");
		gridlayout.addView(tv);
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		StringBuilder sb=new StringBuilder();
		if(p!=null){
			for(String per:p){
				sb.append(per+"\n");
			}
		}
		tv.setText(sb.toString());
		gridlayout.addView(tv);
		
		
		File f=new File(pinfo.applicationInfo.publicSourceDir);
		long size = Long.valueOf(f.length());
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序大小:");
		gridlayout.addView(tv);
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText((size/1024)+"K");
		gridlayout.addView(tv);
		
		//
		String ss[]=getSingInfo(pinfo.packageName);
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序pubKey:");
		gridlayout.addView(tv);
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText(ss[1]);
		gridlayout.addView(tv);
		
		
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		tv.setText("程序signNumber:");
		gridlayout.addView(tv);
		tv=(TextView) inflater.inflate(R.layout.mytext, null);
		sb.setLength(0);
		for(int i=0;i<ss[0].length();i+=50){
			if(i>ss[0].length()-50){
				sb.append(ss[0].subSequence(i, ss[0].length()));
			}else{
				sb.append(ss[0].subSequence(i, i+50)+"\n");
			}
		}
		tv.setText(sb.toString());
		gridlayout.addView(tv);
		
	}
}
